//
//  SNActivitiesIconCell.m
//  app
//
//  Created by 王少宁 on 15/8/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNActivitiesIconCell.h"



@implementation SNActivitiesIconCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier shopBaseModel:(SNShopBaseModel *)model
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier shopBaseModel:model]) {
        if (model != nil) {
            [self cellWithModel:model];
        }
    }
    return self;
}

- (void)cellWithModel:(SNShopBaseModel *)shopModel
{
    SNShopActivitiesIconModel * model = (SNShopActivitiesIconModel *)shopModel;
    
    if (model.activitiesIconArray.count == 0) return;
    
    self.shopBaseModel = shopModel;
    
    if (self.contentView.subviews.count) {
        [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    CGFloat picW = 50;
    CGFloat itemW = SCREEN_WIDTH / 4;
    CGFloat itemH = model.cellHeight / model.linage;
    CGFloat xSpace = (itemW - picW) / 2;
    
    for (int i = 0; i < model.activitiesIconArray.count; i++) {
        
        UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(i%4 * itemW, i/4 * itemH, itemW, itemH)];
        [self.contentView addSubview:backView];
        
        SNShopActivitiesIconSubModel * subModel = model.activitiesIconArray[i];
        
        UIImageView * picView = [[UIImageView alloc] initWithFrame:CGRectMake(xSpace, 18, picW, picW)];
        if (i == model.activitiesIconArray.count - 1) {
            picView.image = [UIImage imageNamed:@"ic_contact_pressed.png"];
        } else {
            [picView sd_setImageWithURL:[NSURL URLWithString:subModel.pic]];
        }
        
        [backView addSubview:picView];
        
        UILabel * titleLabel = CREATE_WITH_FRAME_OF(UILabel, [picView.ufo fx:0 fy:1.0], [picView.ubo fw:1.0 oh:itemH - picView.ufo.yy]);
        titleLabel.text = subModel.name;
        titleLabel.font = JKAppGlobleFontMinTagText;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [XCommon hexStringToColor:kColor_gray_text];
        [backView addSubview:titleLabel];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = backView.bounds;
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:btn];
    }
    
}

- (void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(itemClickWithShopBaseModel:row:index:)]) {
        [self.delegate itemClickWithShopBaseModel:self.shopBaseModel row:0 index:btn.tag-1];
    }
}

@end
